package com.kanshu.ksgb.fastread.doudou.module.signin.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.Xutils;


/**
 * Title: ChipView.java
 * Description: 自定义小碎片下落动画
 * Copyright: Copyright(c)2017
 * Company: 成都古羌科技有限公司
 * CreateTime: 2017/12/21 9:42
 *
 * @author EvanHe
 * @version 1.0
 */
public class ChipView extends View {
    private static final int NUM_CHIPFLAKES = 40; // 碎片数量
    private static final int DELAY = 5; // 延迟
    private ChipFlake[] mChipFlakes; // 碎片

    Resources res = Xutils.getContext().getResources();
    Bitmap bmp1 = BitmapFactory.decodeResource(res, R.mipmap.ic_chip_1);
    Bitmap bmp2 = BitmapFactory.decodeResource(res, R.mipmap.ic_chip_2);
    Bitmap bmp3 = BitmapFactory.decodeResource(res, R.mipmap.ic_chip_3);
    Bitmap bmp4 = BitmapFactory.decodeResource(res, R.mipmap.ic_chip_4);
    Bitmap bmp5 = BitmapFactory.decodeResource(res, R.mipmap.ic_chip_5);
    Bitmap bmp6 = BitmapFactory.decodeResource(res, R.mipmap.ic_chip_6);
    Bitmap bmp7 = BitmapFactory.decodeResource(res, R.mipmap.ic_chip_7);

    public ChipView(Context context) {
        super(context);
    }

    public ChipView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ChipView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(21)
    public ChipView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (w != oldw || h != oldh) {
            initChip(w, h);
        }
    }

    private void initChip(int width, int height) {
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); // 抗锯齿
        // paint.setColor(Color.parseColor("#f3c99f")); // 碎片颜色
        // paint.setStyle(Paint.Style.FILL); // 填充;
        mChipFlakes = new ChipFlake[NUM_CHIPFLAKES];
        for (int i = 0; i < NUM_CHIPFLAKES; ++i) {
            mChipFlakes[i] = ChipFlake.create(width, height, paint);
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < mChipFlakes.length; i++) {
            ChipFlake s = mChipFlakes[i];
            if (i < 6) {
                s.draw(canvas, bmp1);
            } else if (i < 10) {
                s.draw(canvas, bmp2);
            } else if (i < 16) {
                s.draw(canvas, bmp3);
            } else if (i < 21) {
                s.draw(canvas, bmp4);
            } else if (i < 27) {
                s.draw(canvas, bmp5);
            } else if (i < 35) {
                s.draw(canvas, bmp6);
            } else if (i < 40) {
                s.draw(canvas, bmp7);
            }
        }
        // 隔一段时间重绘一次, 动画效果
        getHandler().postDelayed(runnable, DELAY);
    }

    // 重绘线程
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            invalidate();
        }
    };

}
